package com.gome.bg.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import com.gome.util.TraceInfo;


public class httpClientUtil {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
      
		
		
		String onlinekey="jbfc35w1n8ffy3uul6qicfvm1c1rw943";
		String cashkey="juwgp6q7rx96121mnv8i0trzxtsodnfl";
		 Map<String, String> params = new HashMap<String, String>();
         params.put("apply_time", "2013-10-07 10:30:00");
         params.put("notify_time", "2013-10-07 10:30:00");
         params.put("refund_to","BANK");
         params.put("sign_type", "MD5");
         params.put("external_trade_no", "64769812");
         params.put("refund_to_account", "alipaytest001@alipay.com");
         params.put("success_time", "2013-10-08 10:30:00");
         params.put("error_code", "ILLEGAL_SIGN");
         params.put("notify_type", "refund_result");
         params.put("order_id", "2000106402");
         params.put("refund_result", "SUCCESS");
         params.put("refund_to_bank_card", "******3862");
         params.put("sign", "");
         params.put("external_refund_id", "1101602000A3700001");
         params.put("refund_to_bank_name", "工商银行");
         params.put("notify_id", "70fec0c2730b27528665af4517c27b95");
         params.put("failure_time", "2013-10-08 10:30:00");
         params.put("confirm_time", "2013-10-07 10:30:00");
		
         String mysign = sign(params,
        		 onlinekey);
         
		Map<String, String> param = new HashMap<String, String>();
		param.put("apply_time", "2013-10-07 10:30:00");
		param.put("notify_time", "2013-10-07 10:30:00");
		param.put("refund_to", "BANK");
		param.put("external_trade_no", "64769812");
		param.put("refund_to_account", "alipaytest001@alipay.com");
		param.put("success_time", "2013-10-08 10:30:00");
		param.put("error_code", "ILLEGAL_SIGN");
		param.put("notify_type", "refund_result");
		param.put("order_id", "2000106402");
		param.put("refund_result", "SUCCESS");
		param.put("refund_to_bank_card", "******3862");
		param.put("external_refund_id", "1101602000A3700001");
		param.put("refund_to_bank_name", "工商银行");
		param.put("notify_id", "70fec0c2730b27528665af4517c27b95");
		param.put("failure_time", "2013-10-08 10:30:00");
		param.put("confirm_time", "2013-10-07 10:30:00");
		param.put("sign", "6e5fb09dc41bbb9180f8f8bf1a6acb04");
		param.put("sign_type", "MD5");
		String url = "http://rb.gome.com.cn:8480/refund/alipayrefund.do?";
		HttpClient httpclient = new HttpClient();
		PostMethod method = new PostMethod(url);

		method.addParameters(generatNameValuePair(param));
		method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=utf-8");
		method.addRequestHeader("User-Agent", "Mozilla/4.0");

		String result = null;
		httpclient.executeMethod(method);
		result = new String(method.getResponseBody(), "utf-8");
		
	}
	
	/**
	 * MAP类型数组转换成NameValuePair类型
	 * 
	 * @param properties
	 *            MAP类型数组
	 * @return NameValuePair类型数组
	 */
	public static NameValuePair[] generatNameValuePair(
			Map<String, String> properties) {
		NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
		int i = 0;
		for (Map.Entry<String, String> entry : properties.entrySet()) {
			nameValuePair[i++] = new NameValuePair(entry.getKey(),
					entry.getValue());
		}

		return nameValuePair;
	}

	public static String sign(Map params, String privateKey) {
		Properties properties = new Properties();

		for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			Object value = params.get(name);

			if (name == null || name.equalsIgnoreCase("sign")
					|| name.equalsIgnoreCase("sign_type")) {
				continue;
			}

			properties.setProperty(name, value.toString());

		}

		String content = getSignatureContent(properties);
		 TraceInfo.traceInfo(content);
		return sign(content, privateKey); 
	}
	
	public static String getSignatureContent(Properties properties) {
		StringBuffer content = new StringBuffer();
		List keys = new ArrayList(properties.keySet());
		Collections.sort(keys);

		for (int i = 0; i < keys.size(); i++) {
			String key = (String) keys.get(i);
			String value = properties.getProperty(key);

			content.append((i == 0 ? "" : "&") + key + "=" + value);
		}

		return content.toString();
	}
	
	public static String sign(String content, String privateKey) {
		if (privateKey == null) {
			return null;
		}
		String signBefore = content + privateKey;
			return Md5Encrypt.md5(signBefore);

	}

}
